/* 
 *  Copyright (C) 2007 Dip. Ing. dell'Informazione, University of Pisa, Italy
 *  http://info.iet.unipi.it/~cng/ns2voip/
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA, USA
 */
/*
 */

#include <voip_bidirectional.h>
#include <voip_source.h>
#include <ranvar.h>
#include <stdio.h>

static class VoipBidirectionalClass : public TclClass {
public:
   VoipBidirectionalClass() : TclClass("VoipBidirectional") {}
   TclObject* create(int, const char*const*) {
      return (new VoipBidirectional);
   }
} class_voip_bidirectional;

VoipBidirectional::VoipBidirectional ()
{
	source_[0]   = 0;
	source_[1]   = 0;
	nsources_    = 0;
	debug_       = false;
	initialized_ = false;
}

int
VoipBidirectional::command (int argc, const char*const* argv)
{
	if ( argc == 2  && strcmp (argv[1], "debug") == 0 ) {
		debug_ = true;
		return TCL_OK;

	} else if ( argc == 2  && strcmp (argv[1], "nodebug") == 0 ) {
		debug_ = false;
		return TCL_OK;

	} else if ( argc == 3 && strcmp (argv[1], "source") == 0 ) {
		if ( nsources_ == 2 ) {
			fprintf (stderr, "You already bound TWO sources to this object\n");
			return TCL_ERROR;
		}
		source_[nsources_] = (VoipSource*) TclObject::lookup (argv[2]);
		++nsources_;
		return TCL_OK;

	}
	return TCL_ERROR;
}

static class VoipBidirectionalItuP59Class : public TclClass {
public:
   VoipBidirectionalItuP59Class() : TclClass("VoipBidirectionalItuP59") {}
   TclObject* create(int, const char*const*) {
      return (new VoipBidirectionalItuP59);
   }
} class_voip_bidirectional_itu_p59;

VoipBidirectionalItuP59::VoipBidirectionalItuP59 () :
	//timer_ (this),
	singleTalkA_   (1.004),
	singleTalkB_   (1.004),
	doubleTalk_    (0.228),
	mutualSilence_ (0.508)
{
	// Nihil.
}

/*
void
VoipBidirectional::handle ()
{
	if ( ! initialized () ) {
		fprintf (stderr, "VoIP bidirectional not initialized");
		abort ();
	}

	double talk;
	double silence;

	// If there is a bidirectional module bound to this object
	// then get the next talkspurt and silence periods from it.
	if ( bidirectional_ ) {
		bidirectional_->next (this, talk, silence);

	// Otherwise, draw the talkspurt and silence period duration randomly
	// from the random variables depending on the source model.
	} else {
		talk = talk_->value();
		silence = silence_->value();
	}

	if ( debug_ ) fprintf (stderr, "%.9f new talkspurt %f, %f (ID %d)\n",
		NOW, talk, silence, count_);

	// Tell the encoder that a new talkspurt is to be generated.
	encoder_->talkspurt (count_, talk);

	// Update the talkspurt counter.
	++count_;

	// Reschedule this function to be called after the silence period.
	timer_.start (talk + silence);
}
*/
